package com.diadraw.extensions.dropbox
{
	/**
	 * @author Radoslava Leseva, Radoslava.Leseva@diadraw.com
	 * http://diadraw.com
	 * http://ideadrawapp.com
	 * 
	 * */
	
	import flash.desktop.NativeApplication;
	import flash.events.DataEvent;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.InvokeEvent;
	import flash.events.StatusEvent;
	import flash.external.ExtensionContext;
	import flash.net.URLVariables;
	
	
	final public class NativeDropboxLogin extends EventDispatcher
	{
		protected var extensionContext : ExtensionContext;
		private static const EXTENSION_ID : String = "com.diadraw.extensions.dropbox.NativeDropboxLogger";
		
		public static const EXT_EVENT : String = "EXT_EVENT";
		
		public function NativeDropboxLogin(target:IEventDispatcher=null)
		{
			super(target);
			
			NativeApplication.nativeApplication.removeEventListener( InvokeEvent.INVOKE, onInvoke );
			NativeApplication.nativeApplication.addEventListener( InvokeEvent.INVOKE, onInvoke );
		}
		
		
		private function ensureContext() : void
		{
			if ( null != extensionContext )
			{
				dispatchEvent( new DataEvent( EXT_EVENT, false, false, "Context already initialised" ) );
				return;
			}
			
			
			extensionContext = ExtensionContext.createExtensionContext( EXTENSION_ID, null);
			
			dispatchEvent( new DataEvent( EXT_EVENT, false, false, "Instantiating context..." ) );
			try
			{
				extensionContext = ExtensionContext.createExtensionContext( EXTENSION_ID, null );
				
				if ( null != extensionContext )
				{
					dispatchEvent( new DataEvent( EXT_EVENT, false, false, "Context instantiated!" ) );
				}
				else
				{
					dispatchEvent( new DataEvent( EXT_EVENT, false, false, "Context is null..." ) );
				}
			}
			catch ( error : ArgumentError )
			{
				dispatchEvent( new DataEvent( EXT_EVENT, false, false, "Error: " + error.toString() ) );
			}
		}
		
		
		public function linkWithDropbox( appKey : String, appSecret : String, root : String ) : void
		{
			ensureContext();
			
			if ( null != extensionContext )
			{
				extensionContext.addEventListener( StatusEvent.STATUS, onStatusEvent);
				extensionContext.call( "as_linkWithDropbox", appKey, appSecret, root );
			}
		}
		
		
		public function unlinkFromDropbox() : void
		{
			ensureContext();
			
			if ( null != extensionContext )
			{
				extensionContext.addEventListener( StatusEvent.STATUS, onStatusEvent);
				extensionContext.call( "as_unlinkFromDropbox" );
			}
		}
		
		
		private function onInvoke( _event : InvokeEvent ) : void
		{
			if ( null == _event.arguments )
			{
				return;
			}
			
			if ( 0 == _event.arguments.length )
			{
				return;
			}
			
			var url : String = _event.arguments[ 0 ];
			
			if ( null == url )
			{
				return;
			}
			
			if ( -1 != url.indexOf( "cancel" ) )
			{
				dispatchEvent( new NativeDropboxLinkEvent( NativeDropboxLinkEvent.DROPBOX_LINK_CANCELLED ) );
				return;
			}
			
			if ( -1 == url.indexOf( "connect" ) )
			{
				return;
			}
			
			var idx : Number = url.indexOf( "?" );
			if ( -1 == idx )
			{
				return;
			}
			
			var urlArgs : String = url.slice( idx + 1, url.length );
			
			var urlVars : URLVariables = new URLVariables();
			urlVars.decode( urlArgs );
			
			var userId	 	: String = urlVars.uid;
			var valSecret 	: String = urlVars.oauth_token_secret;
			var valKey 		: String = urlVars.oauth_token;
			
			dispatchEvent( new NativeDropboxLinkEvent( NativeDropboxLinkEvent.DROPBOX_LINKED, valKey, valSecret, userId ) );		
		}
		
		
		private function onStatusEvent( event : StatusEvent ) : void
		{
			dispatchEvent( new NativeDropboxLinkEvent( event.level ));
		}
	}
}